//
//  ZGWJiBingFengXianJiuZhengThirdTableViewCell.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/6/6.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ZGWJiBingFengXianJiuZhengThirdTableViewCell.h"

@implementation ZGWJiBingFengXianJiuZhengThirdTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withShengHuoXiGuanNameArray:(NSMutableArray *)array shengHuoXiGuanDic:(NSMutableDictionary *)dic
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.nameArray = [NSMutableArray arrayWithArray:array];
        [self addMySubviewWithShengHuoXiGuanNameArray:array shengHuoXiGuanDic:dic];
    }
    return self;
}

// 布局
- (void)addMySubviewWithShengHuoXiGuanNameArray:(NSMutableArray *)shengHuoXiGuanNameArray shengHuoXiGuanDic:(NSMutableDictionary *)shengHuoXiGuanDic
{
    // 记录下一个疾病开始的位置
    CGFloat nextShengHuoXiGuanStartY = 0.0;
    for (int i = 0; i < shengHuoXiGuanNameArray.count; i++) {
        UILabel *shengHuoXiGuanNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(25 * kAdjustParamater, 10 * kAdjustParamater + nextShengHuoXiGuanStartY, 70 * kAdjustParamater, 20 * kAdjustParamater)];
     //   shengHuoXiGuanNameLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        shengHuoXiGuanNameLabel.text = shengHuoXiGuanNameArray[i];
        shengHuoXiGuanNameLabel.textColor = [UIColor blackColor];
        shengHuoXiGuanNameLabel.textAlignment = NSTextAlignmentLeft;
        shengHuoXiGuanNameLabel.font = [UIFont systemFontOfSize:k17Big];
        shengHuoXiGuanNameLabel.tag = 500 + i;
        shengHuoXiGuanNameLabel.numberOfLines = 0;
        // 自适应高度
        CGRect newShengHuoLabel = shengHuoXiGuanNameLabel.frame;
        newShengHuoLabel.size.height = [[NetStatusHandle shareHandle] adjustHeight:shengHuoXiGuanNameLabel.text size:17 width:70];
        shengHuoXiGuanNameLabel.frame = newShengHuoLabel;
        
        nextShengHuoXiGuanStartY = nextShengHuoXiGuanStartY + shengHuoXiGuanNameLabel.height + 25 * kAdjustParamater;
        [self.contentView addSubview:shengHuoXiGuanNameLabel];
        
        // 经常前方的button
        UIButton *jingChangButton = [UIButton buttonWithType:UIButtonTypeSystem];
        jingChangButton.frame = CGRectMake(125 * kAdjustParamater, shengHuoXiGuanNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        jingChangButton.layer.cornerRadius = jingChangButton.width / 2;
        jingChangButton.layer.masksToBounds = YES;
        jingChangButton.tag = 100 + i;
        [jingChangButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:jingChangButton];
        
        // 经常label
        UILabel *jingChangLabel = [[UILabel alloc] initWithFrame:CGRectMake(jingChangButton.right + 10 * kAdjustParamater, shengHuoXiGuanNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        jingChangLabel.text = @"经常";
        jingChangLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:jingChangLabel];
        
        // 偶尔前方的button
        UIButton *ouErButton = [UIButton buttonWithType:UIButtonTypeSystem];
        ouErButton.frame = CGRectMake(jingChangLabel.right + 17 * kAdjustParamater, shengHuoXiGuanNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        ouErButton.layer.cornerRadius = jingChangButton.width / 2;
        ouErButton.layer.masksToBounds = YES;
        ouErButton.tag = 200 + i;
        [ouErButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:ouErButton];
        
        // 偶尔label
        UILabel *ouErLabel = [[UILabel alloc] initWithFrame:CGRectMake(ouErButton.right + 10 * kAdjustParamater, shengHuoXiGuanNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        ouErLabel.text = @"偶尔";
        ouErLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:ouErLabel];
        
        // 无前方的button
        UIButton *wuButton = [UIButton buttonWithType:UIButtonTypeSystem];
        wuButton.frame = CGRectMake(ouErLabel.right + 17 * kAdjustParamater, shengHuoXiGuanNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        wuButton.layer.cornerRadius = wuButton.width / 2;
        wuButton.layer.masksToBounds = YES;
        wuButton.tag = 300 + i;
        [wuButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:wuButton];
        
        // 无label
        UILabel *wuLabel = [[UILabel alloc] initWithFrame:CGRectMake(wuButton.right + 10 * kAdjustParamater, shengHuoXiGuanNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        wuLabel.text = @"否";
        wuLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:wuLabel];
        
        // 获取对应生活习惯的数值
        NSString *shengHuoXiGuanCount = [shengHuoXiGuanDic valueForKey:shengHuoXiGuanNameArray[i]];
        if ([shengHuoXiGuanCount isEqualToString:@"1"]) {
            // 经常
            [jingChangButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            [ouErButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            jingChangLabel.textColor = [UIColor blackColor];
            ouErLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        } else if ([shengHuoXiGuanCount isEqualToString:@"2"]) {
            // 偶尔
            [jingChangButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [ouErButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            jingChangLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            ouErLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        } else if ([shengHuoXiGuanCount isEqualToString:@"3"]) {
            // 无
            [jingChangButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [ouErButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            jingChangLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            ouErLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
        }
    }
    // 取出最后一个生活习惯label
    UILabel *lastShengHuoXiGuanLabel = (UILabel *)[self.contentView viewWithTag:499 + shengHuoXiGuanNameArray.count];
    
    self.bottomGrayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, lastShengHuoXiGuanLabel.bottom + 30 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    self.bottomGrayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.07];
    [self.contentView addSubview:_bottomGrayLine];
}

// button的点击事件
- (void)click:(UIButton *)button
{
   // NSLog(@"button.tag = %ld", (long)button.tag);
    NSInteger row = 0;
    if (button.tag >=100 && button.tag < 200) {
        // 说明点击的经常
        row = button.tag - 100;
    } else if (button.tag >=200 && button.tag < 300) {
        // 说明点击的偶尔
        row = button.tag - 200;
    } else if (button.tag >=300) {
        // 说明点击的无
        row = button.tag - 300;
    }
    NSString *shengHuoXiGuanName = self.nameArray[row];
    if ([self.delegate respondsToSelector:@selector(clickTag:withShengHuoXiGuanName:)]) {
        [self.delegate clickTag:button.tag withShengHuoXiGuanName:shengHuoXiGuanName];
    }
}

@end
